package com.zyb.warehouse.controller;

import com.zyb.warehouse.common.Result;
import com.zyb.warehouse.dto.AssignRoleDto;
import com.zyb.warehouse.dto.Page;
import com.zyb.warehouse.entity.Role;
import com.zyb.warehouse.entity.User;
import com.zyb.warehouse.service.UserService;
import com.zyb.warehouse.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * Description:用户管理控制层
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页查询用户
     *
     * @param page
     * @param user
     * @return
     */
    @GetMapping("/user-list")
    public Result userPage(Page page, User user) {
        Page userPageList = userService.queryPageUser(page, user);
        return Result.ok(userPageList);
    }

    /**
     * 添加用户
     *
     * @param user
     * @param token
     * @return
     */
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        Result result = userService.addUser(user, token);
        return result;
    }

    /**
     * 用户启用或者禁用
     *
     * @param user
     * @param token
     * @return
     */
    @PutMapping("/updateState")
    public Result userStartOrStop(@RequestBody User user, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        Result result = userService.userStartOrStop(user, token);
        return result;
    }

    /**
     * 根据用户ID查询用户对应的角色
     *
     * @param userId
     * @return
     */
    @GetMapping("/user-role-list/{userId}")
    public Result queryUserRoleList(@PathVariable("userId") Integer userId) {
        List<Role> roleList = userService.queryUserRoleByUserId(userId);
        return Result.ok(roleList);
    }

    /**
     * 给用户分配角色
     *
     * @param assignRoleDto
     * @return
     */
    @PutMapping("/assignRole")
    public Result userAssignRole(@RequestBody AssignRoleDto assignRoleDto) {
        userService.userAssignRole(assignRoleDto);
        return Result.ok("分配角色成功");
    }

    /**
     * 修改用户
     *
     * @param user
     * @param token
     * @return
     */
    @PutMapping("/updateUser")
    public Result modifyUser(@RequestBody User user, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        Result result = userService.modifyUser(user, token);
        return result;
    }

    /**
     * 删除单个用户
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/deleteUser/{userId}")
    public Result deleteOneUser(@PathVariable("userId") Integer userId) {
        List<Integer> userIds = Arrays.asList(userId);
        Result result = userService.removeUser(userIds);
        return result;
    }

    /**
     * 批量删除用户
     *
     * @param userIds
     * @return
     */
    @DeleteMapping("/deleteUserList")
    public Result batchDeleteUser(@RequestBody List<Integer> userIds) {
        Result result = userService.removeUser(userIds);
        return result;
    }

    /**
     * 重置密码(123456)
     *
     * @param userId
     * @return
     */
    @PutMapping("/updatePwd/{userId}")
    public Result restPassword(@PathVariable("userId") Integer userId, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        Result result = userService.restPassword(userId,token);
        return result;
    }
}
